// components/roomLabel/roomLabel.js
// import Area
const cloud = require('../../utils/cloudFunction')
const con = require('../../utils/control')

Component({
    /**
     * 组件选项
     * 组件与页面样式隔离
     * 或在组件的 .json 文件中新增如下配置
     *     { "styleIsolation": "isolated" }
     */
    options: ({
        styleIsolation: 'isolated'
    }),

    /**
     * 组件的属性列表
     */
    properties: {
        roomName: { // 设备所在区域名称
            type: String,
            value: "roomName"
        },
        roomMac: { // 设备所在区域mac
            type: Number,
            value: 0
        },
        roomId: {
            type: String,
            value: "roomId"
        },
        roomStatus: {
            type: Number,
            value: 50
        },
        devNumber: {
            type: Number,
            value: 0
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        clock: '',
        label: {
            // 禁用下的样式颜色 style
            background: "#363636", // 标签背景色
            switch: "#000000", // 开关背景色
            sliderActiveColor: "#777777", // 滑块左边颜色
            sliderBlockColor: "#898989", // 滑块左边颜色
            sliderBackgroundColor: "#4E4E4E", // 滑块右边颜色
            sliderDisable: true, // 滑块状态 true 为滑块不可选择 sliderDisable = true means no work
        },
        switchLock: true
    },

    /**
     * 组件的内部生命周期，详见 开发文档 - 组件生命周期
     * https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/lifetimes.html
     */
    lifetimes: {
        // 渲染组件时，按外部传值更新组件样式
        ready() {
            this.data.clock = setInterval(() => {
                this.refreshLayout()
                this.updateDev()
            }, 500);
        },

        detached() {
            // 在组件实例被从页面节点树移除时执行           // 
            clearInterval(this.data.clock);
        },
    },

    /**
     * 组件的所在页面的生命周期，详见 开发文档 - 组件生命周期
     * https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/lifetimes.html
     */
    pageLifetimes: {
        show() {

        },
        hide() {

        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        updateDev() {
            if (this.properties.devNumber > 0) {
                this.setData({
                    switchLock: false
                })
            }
        },
        refreshLayout() {
            if (this.properties.roomStatus) {
                this.setData({
                    label: {
                        // active style
                        background: "(left, #0678AC, #33C9EA)",
                        switch: "#2E8298",
                        sliderActiveColor: "#E9F2F1",
                        sliderBlockColor: "#FFFFFF",
                        sliderBackgroundColor: "#20758A",
                        sliderDisable: false,
                    }
                })
            } else {
                this.setData({
                    label: {
                        // disable style
                        background: "#363636",
                        switch: "#494B4A",
                        sliderActiveColor: "#777777",
                        sliderBlockColor: "#898989",
                        sliderBackgroundColor: "#4E4E4E",
                        sliderDisable: true,
                    },
                })
            }
        },

        // 打开详细控制面板，此处预留，待完善 => 开关打开时才能进入控制面板，关闭则不可控制
        openColorPanel() {
            if (this.properties.roomStatus > 0) {

                let status = 'lampStatus=' + this.properties.roomStatus + '&&'
                let id = 'roomId=' + this.properties.roomId + '&&'
                let name = 'title=' + this.properties.roomName + '&&'
                let roomMac = 'roomMac=' + this.properties.roomMac + '&&'
                wx.navigateTo({
                    url: '/pages/index/panel/cctPanel/cctPanel?' + status + id + name + roomMac,
                })

            } else {
                wx.showToast({
                    icon: 'error',
                    title: '请先打开按钮',
                    duration: 2000
                })
            }
            // console.log("bindtap home roomCard")

        },
        // 区域设备控制开关
        roomDevAllControl(e) {
            if (e.detail.value == false) {
                cloud.roomControl(this.properties.roomId, null, 0)
                this.setData({
                    roomStatus: 0,
                })
                con.turnOff(this.properties.roomMac)
            } else {
                // 判断区域下 有没有设备
                if (this.properties.devNumber) {
                    cloud.roomControl(this.properties.roomId, null, 50)
                    this.setData({
                        roomStatus: 50,
                    })
                    con.turnOn(this.properties.roomMac)
                } else {
                    wx.showToast({
                        icon: 'error',
                        title: '请添加设备',
                        duration: 2000
                    })
                    // this.setData({
                    //     roomStatus: 0,
                    // })
                }
            }
        },

        setRoomBrightness(e) {
            var value = e.detail.value
            this.properties.roomStatus = value
            // this.setData({
            //     devBrightness: value
            // })
            setTimeout(() => {
                con.setBrightness(this.properties.roomMac, value)
            }, 500);
        },

        /**
         * 多个灯 调亮度 拖动结束才控制
         * @param {object} e 
         */
        setRoomBrightnessDone(e) {
            var value = e.detail.value
            this.setData({
                roomStatus: value
            })
            cloud.roomControl(this.properties.roomId, null, value)
        },

        // 啥事也不干
        nothing(e) {
            if (this.data.switchLock) {
                wx.showToast({
                    icon: 'error',
                    title: '请先添加设备',
                    duration: 2000
                })
            } else {
                // console.log('nothing')
            }
        },

    },

})